<template>
  <template v-if="mode === 'edit'">
    <el-form ref="cnkFormRef" :model="cnkDataRef" label-width="90px" :rules="cnkFormRules">
      <el-row :gutter="20">
        <el-col :span="12">
          <el-form-item label="网吧编码" prop="bar_code">
            <el-input v-model="cnkDataRef.bar_code" placeholder="请输入网吧编码" clearable />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="校验码" prop="check_code">
            <el-input v-model="cnkDataRef.check_code" placeholder="请输入校验码" clearable />
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="20">
        <el-col :span="12">
          <el-form-item label="终端数量" prop="pc_count">
            <el-input v-model="cnkDataRef.pc_count" placeholder="请输入终端数量" clearable />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="法人代表" prop="legal">
            <el-input v-model="cnkDataRef.legal" placeholder="请输入法人代表" clearable />
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="20">
        <el-col :span="12">
          <el-form-item label="负责人" prop="incharge">
            <el-input v-model="cnkDataRef.incharge" placeholder="请输入负责人" clearable />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="负责人电话" prop="cellphone">
            <el-input v-model="cnkDataRef.cellphone" placeholder="请输入负责人电话" clearable />
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-form-item>
          <el-button @click="handleDialogReset">清 空</el-button>
          <el-button type="primary" @click="handleDialogSave">保 存</el-button>
        </el-form-item>
      </el-row>
    </el-form>
  </template>
  <template v-else>
    <div class="icafe-cnk">
      <el-descriptions :column="2" border>
        <el-descriptions-item label="网吧编码">{{ cnkDataRef.bar_code }}</el-descriptions-item>
        <el-descriptions-item label="校验码">{{ cnkDataRef.check_code }}</el-descriptions-item>
        <el-descriptions-item label="终端数量">{{ cnkDataRef.pc_count }}</el-descriptions-item>
        <el-descriptions-item label="法人代表">{{ cnkDataRef.legal }}</el-descriptions-item>
        <el-descriptions-item label="负责人">{{ cnkDataRef.incharge }}</el-descriptions-item>
        <el-descriptions-item label="负责人电话">{{ cnkDataRef.cellphone }}</el-descriptions-item>
      </el-descriptions>
    </div>
  </template>
</template>

<script lang="ts">
import { defineComponent, nextTick, ref } from 'vue'
import { ElForm, ElMessage } from 'element-plus'
import * as IcafeCnkApi from '@/api/customer/icafe-cnk'
import { copy } from '@/utils/common'
import { NOOP } from '@/utils/helpers'

export default defineComponent({
  name: 'IcafeCnk',
  props: {
    icafeId: {
      type: Number,
      required: true
    },
    mode: String
  },
  setup(props) {
    const cnkFormRef = ref<InstanceType<typeof ElForm>>()
    const defaultCnkData = {
      bar_code: '',
      check_code: '',
      pc_count: '',
      legal: '',
      incharge: '',
      cellphone: ''
    }
    const cnkDataRef = ref({ ...defaultCnkData })

    const cnkFormRules = {
      pc_count: [{ pattern: /^[1-9]\d*$/, message: '必须为大于 1 的整数', trigger: 'blur' }],
      cellphone: [{ pattern: /^1[3|4|5|6|7|8|9][0-9]{9}$/, message: '手机号格式有误', trigger: 'blur' }]
    }

    // 获取
    const getData = () => {
      IcafeCnkApi.getData({ icafe_id: props.icafeId }).then((res) => {
        copy(cnkDataRef.value, res.data)
      })
    }

    // 请求数据
    getData()

    // 清空按钮事件
    const handleDialogReset = () => {
      cnkDataRef.value = { ...defaultCnkData }
      // 使用 nextTick，防止清空表单时消除验证错误提示
      nextTick(() => {
        cnkFormRef.value?.validate(NOOP)
      })
    }

    // 保存按钮事件
    const handleDialogSave = () => {
      cnkFormRef.value?.validate((isValid) => {
        if (isValid) {
          IcafeCnkApi.saveData({ ...cnkDataRef.value, icafe_id: props.icafeId })
            .then((res) => {
              ElMessage.success('保存成功')
            })
            .catch((error) => {
              ElMessage.error(error)
            })
        }
      })
    }

    return { cnkFormRef, cnkDataRef, cnkFormRules, handleDialogReset, handleDialogSave }
  }
})
</script>

<style lang="less" scoped>
.icafe-cnk :deep {
  .el-descriptions__label {
    width: 20%;
  }
  .el-descriptions__content {
    width: 30%;
  }
}
</style>
